package edu.gettysburg.catapp1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.support.v4.app.NavUtils;

@SuppressLint("NewApi")
public class ViewImg extends Activity {
	private VideoView audioView;
	private Uri audioURI;
	private MediaController mc;

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 * 
	 * Sets up the image view based on source:
	 * - Retrieves image file path from intent
	 * - Sets empty image view in center to image from this path
	 * - If the image has a related text, it shows this as well
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		String message = intent.getStringExtra(getPackageName());

		File f = new File(message);
		
		String suffix = f.getName().substring(f.getName().indexOf("."), f.getName().length());
		
		String relatedPath = f.getPath().replace(suffix, "a.txt");

		File relateFile = new File(relatedPath);

		if(relateFile.exists() && (getResources().getConfiguration().orientation == 1 || getResources().getConfiguration().orientation == 3)) {
			if(getResources().getConfiguration().orientation == 1 || getResources().getConfiguration().orientation == 3) {
				setContentView(R.layout.img_text);
				
				TextView tv = (TextView) findViewById(R.id.textV);
				StringBuilder text = new StringBuilder();

				try {
					BufferedReader br = new BufferedReader(new FileReader(relateFile));
					String line;

					while ((line = br.readLine()) != null) {
						text.append(line);
						text.append('\n');
					}
				}
				catch (IOException e) {
					e.printStackTrace();
				}
				tv.setText(text);
				tv.setGravity(Gravity.CENTER_HORIZONTAL);
				
				ImageView mainimg = (ImageView) findViewById(R.id.imgView);

				Bitmap bm = BitmapFactory.decodeFile(message);
				mainimg.setImageBitmap(bm);

				mainimg.setAdjustViewBounds(true);
			}
		}
		else {
			setContentView(R.layout.view_img1);

			ImageView mainimg = (ImageView) findViewById(R.id.imgView);

			Bitmap bm = BitmapFactory.decodeFile(message);
			mainimg.setImageBitmap(bm);

			mainimg.setAdjustViewBounds(true);
		}
		
		Toast.makeText(getBaseContext(), relatedPath, Toast.LENGTH_SHORT).show();
		//getActionBar().hide();
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 * 
	 * Overridden method: Action Bar is hidden
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.view_img, menu);
		return true;
	}


	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 * 
	 * Overridden method: Action Bar is hidden.
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
